package com.newtype.meeting.client.mvc;

import com.extjs.gxt.ui.client.Registry;
import com.extjs.gxt.ui.client.Style.LayoutRegion;
import com.extjs.gxt.ui.client.event.ButtonEvent;
import com.extjs.gxt.ui.client.event.Events;
import com.extjs.gxt.ui.client.event.Listener;
import com.extjs.gxt.ui.client.event.MenuEvent;
import com.extjs.gxt.ui.client.event.SelectionListener;
import com.extjs.gxt.ui.client.event.WindowEvent;
import com.extjs.gxt.ui.client.mvc.AppEvent;
import com.extjs.gxt.ui.client.mvc.Controller;
import com.extjs.gxt.ui.client.mvc.Dispatcher;
import com.extjs.gxt.ui.client.mvc.View;
import com.extjs.gxt.ui.client.util.Margins;
import com.extjs.gxt.ui.client.widget.Html;
import com.extjs.gxt.ui.client.widget.LayoutContainer;
import com.extjs.gxt.ui.client.widget.MessageBox;
import com.extjs.gxt.ui.client.widget.Viewport;
import com.extjs.gxt.ui.client.widget.button.Button;
import com.extjs.gxt.ui.client.widget.custom.ThemeSelector;
import com.extjs.gxt.ui.client.widget.layout.BorderLayout;
import com.extjs.gxt.ui.client.widget.layout.BorderLayoutData;
import com.extjs.gxt.ui.client.widget.layout.FitLayout;
import com.extjs.gxt.ui.client.widget.menu.Menu;
import com.extjs.gxt.ui.client.widget.menu.MenuItem;
import com.extjs.gxt.ui.client.widget.toolbar.FillToolItem;
import com.extjs.gxt.ui.client.widget.toolbar.LabelToolItem;
import com.extjs.gxt.ui.client.widget.toolbar.SeparatorToolItem;
import com.extjs.gxt.ui.client.widget.toolbar.ToolBar;
import com.google.gwt.user.client.ui.RootPanel;
import com.newtype.meeting.client.AppEvents;
import com.newtype.meeting.client.GMeeting;
import com.newtype.meeting.client.model.Account;
import com.newtype.meeting.client.model.AccountType;
import com.newtype.meeting.client.widget.LoginDialog;
import com.newtype.meeting.client.widget.RegistrationWindow;
import com.newtype.meeting.client.widget.TestPanel;
import com.newtype.meeting.shared.LoginInfo;

public class AppView extends View {
	public static final String VIEWPORT = "viewport";
	public static final String CENTER_PANEL = "center";

	private Viewport viewport;
	private LayoutContainer center;
	private LoginDialog dialog;
	private RegistrationWindow registWindow;

	public AppView(Controller controller) {
		super(controller);
	}

	@Override
	protected void initialize() {
		super.initialize();

		dialog = new LoginDialog();
		dialog.setClosable(false);
		dialog.addListener(Events.Hide, new Listener<WindowEvent>() {
			@Override
			public void handleEvent(WindowEvent be) {
				Dispatcher.forwardEvent(AppEvents.Init);
				Dispatcher.forwardEvent(AppEvents.ViewMenu);
			}
		});
		dialog.show();
	}

	@Override
	protected void handleEvent(AppEvent event) {
		if (event.getType() == AppEvents.Init) {
			initUI();
		} else if (event.getType() == AppEvents.LoginHide) {
			onLoginHide();
		} else if (event.getType() == AppEvents.LoginReset) {
			onLoginReset();
		} else if (event.getType() == AppEvents.ToGoogleRegister) {
			onToGoogleRegister(event);
		}
	}

	private void onToGoogleRegister(AppEvent event) {
		LoginInfo loginInfo = event.getData();
		registWindow = new RegistrationWindow(loginInfo);
		registWindow.show();
	}

	private void onLoginReset() {
		dialog.reset();
	}

	private void onLoginHide() {
		dialog.hide();
	}

	private void initUI() {
		viewport = new Viewport();
		viewport.setLayout(new BorderLayout());

		createNorth();
		createCenter();

		//Registry serves as a global context
		Registry.register(VIEWPORT, viewport);
		Registry.register(CENTER_PANEL, center);

		RootPanel.get().add(viewport);
	}

	private void createCenter() {
		center = new LayoutContainer();
		center.setLayout(new FitLayout());

		BorderLayoutData centerData = new BorderLayoutData(LayoutRegion.CENTER);
		centerData.setMargins(new Margins(5));
		center.setHeight(500);

		//center.add(new CascadeComboBoxExample());
		center.add(new TestPanel());

		viewport.add(center, centerData);
	}

	private void createNorth() {
		ToolBar toolBar = new ToolBar();
		toolBar.setStateful(false);
		toolBar = new ToolBar();

		Button item0 = new Button("会议管理首页");
		item0.setIconStyle("icon-meeting");
		item0.addSelectionListener(new SelectionListener<ButtonEvent>() {
			@Override
			public void componentSelected(ButtonEvent ce) {
				Dispatcher.forwardEvent(AppEvents.ViewMeeting);
			}
		});

		Button item1 = new Button("发起会议");
		item1.setIconStyle("icon-add");
		item1.addSelectionListener(new SelectionListener<ButtonEvent>() {
			@Override
			public void componentSelected(ButtonEvent ce) {
				Dispatcher.forwardEvent(AppEvents.ViewStartMeeting);
			}
		});

		Button item2 = new Button("发起会议列表");
		item2.setIconStyle("icon-table");
		item2.addSelectionListener(new SelectionListener<ButtonEvent>() {
			@Override
			public void componentSelected(ButtonEvent ce) {
				Dispatcher.forwardEvent(AppEvents.ViewStartMeetingList);
			}
		});

		Button item3 = new Button("会议管理");
		item3.setIconStyle("icon-application");
		item3.addSelectionListener(new SelectionListener<ButtonEvent>() {
			@Override
			public void componentSelected(ButtonEvent ce) {
				Dispatcher.forwardEvent(AppEvents.ViewMeetingManagement);
			}
		});

		toolBar.add(item0);
		toolBar.add(new SeparatorToolItem());
		toolBar.add(item1);
		toolBar.add(new SeparatorToolItem());
		toolBar.add(item2);
		if (((Account) Registry.get(GMeeting.ACCOUNT)).getAccountType().equals(
				AccountType.ADMIN)) {
			toolBar.add(new SeparatorToolItem());
			toolBar.add(item3);
		}
		toolBar.add(new SeparatorToolItem());
		toolBar.add(new ThemeSelector());
		toolBar.add(new FillToolItem());

		toolBar.add(new LabelToolItem("欢迎 "));
		Button personal = new Button("<span class='span-text'>"
				+ ((Account) Registry.get(GMeeting.ACCOUNT)).getFullName()
				+ "</span>");

		// 修改个人资料按钮
		Menu menu = new Menu();
		MenuItem menuItem = new MenuItem("修改我的资料");
		menuItem.setIconStyle("icon-vcard_edit");
		menuItem.addSelectionListener(new SelectionListener<MenuEvent>() {
			@Override
			public void componentSelected(MenuEvent ce) {
				Dispatcher.forwardEvent(AppEvents.ToVcardEdit,
						((Account) Registry.get(GMeeting.ACCOUNT)).getId());
			}
		});
		menu.add(menuItem);

		personal.setMenu(menu);
		toolBar.add(personal);

		if (((Account) Registry.get(GMeeting.ACCOUNT)).getAccountType().equals(
				AccountType.ADMIN)) {
			toolBar.add(new SeparatorToolItem());
			toolBar.add(getAdminMenu());
		}

		toolBar.add(new SeparatorToolItem());

		//退出登录按钮
		final Button logout = new Button("<span class='span-text'>退出登录</span>");
		logout.setIconStyle("icon-logout");
		logout.addSelectionListener(new SelectionListener<ButtonEvent>() {
			@Override
			public void componentSelected(ButtonEvent ce) {
				MessageBox.alert("提示", "退出登录", null);
			}
		});
		toolBar.add(logout);

		LoginInfo loginInfo =Registry.get(GMeeting.LOGININFO);
		if (loginInfo != null) {
			Html logoutLink = new Html("<a href='" + loginInfo.getLogoutUrl()
					+ "' alt='退出登录'>退出登录</a>");
			toolBar.add(logoutLink);
		}

		BorderLayoutData northData = new BorderLayoutData(LayoutRegion.NORTH,
				33);
		northData.setMargins(new Margins());
		viewport.add(toolBar, northData);
	}

	private Button getAdminMenu() {
		Button item = new Button("Administration");

		Menu menu = new Menu();

		MenuItem menuItem = new MenuItem("人员维护");
		menuItem.setIconStyle("icon-system");
		menuItem.addSelectionListener(new SelectionListener<MenuEvent>() {
			@Override
			public void componentSelected(MenuEvent ce) {
				Dispatcher.forwardEvent(AppEvents.ViewAccountManagement);
			}
		});
		menu.add(menuItem);

		menuItem = new MenuItem("部门管理");
		menuItem.setIconStyle("icon-system");
		menuItem.addSelectionListener(new SelectionListener<MenuEvent>() {
			@Override
			public void componentSelected(MenuEvent ce) {
				Dispatcher.forwardEvent(AppEvents.ViewDepartmentManagement);
			}
		});
		menu.add(menuItem);

		menuItem = new MenuItem("职称管理");
		menuItem.setIconStyle("icon-system");
		menuItem.addSelectionListener(new SelectionListener<MenuEvent>() {
			@Override
			public void componentSelected(MenuEvent ce) {
				Dispatcher.forwardEvent(AppEvents.ViewTitleManagement);
			}
		});
		menu.add(menuItem);

		item.setMenu(menu);

		return item;
	}

}
